const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      pickerOptions: pickerOptions,
      rules: {
        Carousel_Chart_Name: [{
          required: true,
          message: '请输入轮播图名称',
          trigger: 'blur',
        }],
        Sales_Unit_Price_Detail_SerialNumber: [{
          required: true,
          message: '请选择销售订单',
          trigger: 'change',
        }],
      },
      // fileList2: [{ name: 'food.jpeg', url: '../../../../admin/images/act.jpg' }],
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: true,
      App_ID: [],
      SalesInfo: [],
      shop: [],
      form: {
        Carousel_Chart_SerialNumber: '', //轮播图助主键
        Carousel_Chart_Name: '',   //轮播图名称
        Sales_Unit_Price_Detail_SerialNumber: '',   //销售单价流水号
        Picture_URL: '',    //轮播图图片
        Carousel_Chart_Path:'',
        Carousel_Chart_Type:'',
        App_ID: 'wx752cd7c5466abbaf',    //小程序appid
        Store_SerialNumber: '',
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        pic: '',
      },
      loading: false,
      AddressList: [],
    }
  },
  mounted() {
    this.getList()
    this.getAddress()
  },
  methods: {
    //获取路径
    getAddress() {
      const _this = this
      $.ajax({
        type: 'POST',
        url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),//
        data: {
          Data: "enterprise_appid_path",
          isNullEnterprise:true
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        success: function (Result) {
          _this.AddressList = Result;
          console.log(_this.AddressList);
          console.log(Result);
        },
        dataType: "json"
      });
    },
    changePath(e){//获取地址类型并付给form
      this.form.Carousel_Chart_Type = this.AddressList.filter(item=>item.AppID_Path==e)[0].AppID_Path_Type
      this.form.Sales_Unit_Price_Detail_SerialNumber = ''
    },
    getList() {
      let _this = this
      // 获取小程序APPID
      $.ajax({
        url: DomainName + '/getInquiryAction',
        type: 'POST',
        data: {
          Data: 'enterprise_appid',
          token: $.cookie("token")
        },
        success: function (res) {
          // _this.tableData = res
          _this.App_ID = res
          console.log(res);
        },
        error: function () {
          console.log(error)
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      }),
        // 获取商品
        $.ajax({
          url: DomainName + '/getCarouselSalesUnitPriceAction',
          type: 'POST',
          data: {
            token: $.cookie("token")
          },
          success: function (res) {
            _this.SalesInfo = res
            console.log(res);
          },
          error: function () {
            console.log(error)
          },
          complete: function (XMLHttpRequest) {
            _this.loading = false
            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
              window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
            }
          }
        })
    },
    // 改变商品
    ChangeSales(item) {
      let _this = this
      console.log(item)
      // 获取门店
      $.ajax({
        url: DomainName + '/getCarouselStoreAction',
        type: 'POST',
        data: {
          Sales_Unit_Price_Detail_SerialNumber: item,
          token: $.cookie("token")
        },
        success: function (res) {
          _this.shop = res
          _this.disabled = false
        },
        error: function () {
          console.log(error)
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    submitForm(formName) {
      let _this = this
      _this.form.pic = ''
      console.log(this.form);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          $.ajax({
            url: DomainName + '/getAddAction',
            type: 'POST',
            data: {
              Data: JSON.stringify({ sales_carousel_chart: [_this.form] }),
              token: $.cookie("token")
            },
            success: function (res) {
              console.log('提交结果', res)
              _this.$message.success("提交成功")
              window.location.href = './RotationChart.html'
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }

      });
    },
    // 图片上传
    uploadFront(file) {
      let _this = this
      let num = 1
      if (file.size > 150000 && file.size < 500000) {
        num = 0.4
      } else if (file.size > 500000) {
        num = 0.2
      }
      new Compressor(file, {
        quality: num,
        // if(file.size) {
        // }
        success(file) {
          console.log(file.size)
          const formData = new FormData();
          formData.append('file', file, file.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file);
          oFileReader.onload = function (e) {
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileList',
              data: {
                Data: e.target.result,
                DataLength: file.size,
                FileName: file.name
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                _this.form.pic = e.target.result
                if (Result.Status == "success") {
                  _this.form.Picture_URL = Result.Picture_Url
                  _this.$message.success('上传成功')
                  console.log(Result.Picture_Url);
                } else {
                  _this.$message.error('上传失败')
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    },
    // 查看图片
    handleSpecification(index) {
      this.Specification = index
      console.log(this.Specification);
    },
    handlePictureCardPreview(file) {
      this.dialogVisible = true
      this.dialogImageUrl = file
    },
    // 删除图片
    handleRemove(type, index) {
      if (type === 'front') {
        this.form.pic = ''
      } else if (type === 'banner') {
        this.fileList = this.fileList.filter(function (item) {
          return item.Picture_Order != index
        })
      } else if (type === 'details') {
        this.fileList1 = this.fileList1.filter(function (item) {
          return item.Picture_Order != index
        })
      } else if (type === 'barcode') {
        this.fileList2 = this.fileList2.filter(function (item) {
          return item.Picture_Order != index
        })
      }
      else if (type === 'Specifications') {
        console.log(this.form.Children.article_specifications[0])
        this.form.Children.article_specifications[0].Picture_URL = ''
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
})